package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.VideoListAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.VideoUtil;
import cn.yeyiu.util.bean.VideoInfo;
import cn.yeyiu.videoplayer.VideoPlayerActivity;

public class OtherListActivity extends AppCompatActivity {
    private VideoListAdapter adapter;
    private ImageView back_button;
    private TextView back_title;
    private ImageView btnNext;
    private ImageView btnPre;
    private String cat;
    private View llPage;
    private RecyclerView recyclerView;
    private TextView tvCur;
    private TextView tvMax;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_other_list);
        dataInit();
        viewInit();
        adapterInit();
        eventInit();
        VideoUtil.postOtherVideoList(this.cat, this.adapter.videoInfoList);
    }

    void dataInit() {
        this.cat = getIntent().getStringExtra("ctg");
        this.adapter = new VideoListAdapter(this);
    }

    void viewInit() {
        this.recyclerView = (RecyclerView) findViewById(R.id.video_card_rv);
        this.back_button = (ImageView) findViewById(R.id.back_btn);
        this.back_title = (TextView) findViewById(R.id.back_tv);
        this.btnPre = (ImageView) findViewById(R.id.pre_page_btn);
        this.btnNext = (ImageView) findViewById(R.id.next_page_btn);
        this.tvCur = (TextView) findViewById(R.id.cur_page_tv);
        this.tvMax = (TextView) findViewById(R.id.sum_page_tv);
        this.llPage = findViewById(R.id.page_ll);
        llPage.setVisibility(View.VISIBLE);
    }

    void adapterInit() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
            this.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        } else {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            this.recyclerView.setLayoutManager(linearLayoutManager);
        }
        this.recyclerView.setAdapter(this.adapter);
        this.back_title.setText(this.cat);
    }

    void eventInit() {
        this.back_button.setOnClickListener(v -> {
            finish();
        });
        this.adapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemLongClick(View view) {
            }

            @Override
            public void onItemClick(View view) {
                int childAdapterPosition = recyclerView.getChildAdapterPosition(view);
                VideoInfo videoInfo = adapter.videoInfoList.getValue().get(
                        adapter.cur_page * adapter.page_size + childAdapterPosition
                );
                MediaUtil.playVideo(OtherListActivity.this, VideoPlayerActivity.class, videoInfo);
            }
        });
        this.adapter.videoInfoList.observe(this, videoInfos -> {
            this.adapter.update();
            this.tvMax.setText(this.adapter.getMaxPage());
        });
        this.btnPre.setOnClickListener(v -> {
            this.recyclerView.smoothScrollToPosition(0);
            this.adapter.prePage();
            this.tvCur.setText(this.adapter.getPage());
        });
        this.btnNext.setOnClickListener(v -> {
            this.recyclerView.smoothScrollToPosition(0);
            this.adapter.nextPage();
            this.tvCur.setText(this.adapter.getPage());
        });
    }
}
